package com.etogc.groupchat.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.etogc.groupchat.R;
import com.etogc.groupchat.base.BaseActivity;
import com.etogc.groupchat.common.WebActivity;
import com.etogc.groupchat.login.biz.LoginBiz;
import com.etogc.groupchat.login.biz.LoginHttpBiz;
import com.etogc.http.ReqDataHandler;
import com.etogc.http.ReqErrorHandler;
import com.etogc.http.VolleyHttpUtil;
import com.etogc.util.GcUrl;
import com.etogc.util.IntentHelper;
import com.etogc.util.YcToastHelper;
import com.etogc.view.LoadDialog;

import org.json.JSONObject;

import java.util.Map;

public class RegActivity extends BaseActivity {
    private EditText etTel;
    private EditText etVerify;
    private EditText etPwd;
    private String mType;
    private Button btnReg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        etTel = findViewById(R.id.etTel);
        etVerify = findViewById(R.id.etVerify);
        etPwd = findViewById(R.id.etPwd);
        btnReg = findViewById(R.id.btnReg);
        btnReg.setOnClickListener(listener);
        findViewById(R.id.tvGetVerify).setOnClickListener(listener);
        findViewById(R.id.tv_user_intro).setOnClickListener(listener);
        View llVerify = findViewById(R.id.llVerify);
        showBack();

        mType = getIntent().getStringExtra("type");
        if (TextUtils.isEmpty(mType)) {
            setTitle("注册");
        } else {
            if (mType.equals("regist")) {
                setTitle("注册");
                btnReg.setText("注册");
                llVerify.setVisibility(View.VISIBLE);
            } else {
                setTitle("绑定帐号");
                btnReg.setText("绑定");
                llVerify.setVisibility(View.VISIBLE);
            }
        }
    }

    String mCode = "";

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tvGetVerify:
                    if (mType.equals("regist")) {
                        requestSms();
                    } else {
                        String tel = etTel.getText().toString();
                        LoginHttpBiz.getSms(RegActivity.this, tel, new VolleyHttpUtil.ResponseCallBack() {
                            @Override
                            public void handleResponse(JSONObject response, int errCode) {
                                LoadDialog.dismiss(RegActivity.this);
                                boolean isSuccess = ReqErrorHandler.getInstance().isSuccess(response, true);
                                if (isSuccess) {
                                    Map<String, Object> map = ReqDataHandler.getMapWithEntity(response);
                                    if (map != null) {
                                        String code = String.valueOf(map.get("code"));
                                        if (!"".equals(code) && !"null".equals(code)) {
                                            mCode = code;
                                        }
                                        YcToastHelper.getInstance()._toast("请求验证码成功");
                                    }
                                }
                            }
                        });
                    }
                    break;
                case R.id.btnReg:
                    requestReg();
                    break;

                case R.id.tv_user_intro:
                    jumpWeb();
                    break;
            }

        }
    };

    private void requestSms() {
        String tel = etTel.getText().toString();
        LoginHttpBiz.getSms(this, tel);
    }

    private void jumpWeb() {
        String url = GcUrl.HOST + "/static/agreement.html";
        Intent intent = new Intent(this, WebActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", "用户协议");
        startActivity(intent);
    }

    //需要服务端返回token
    private void requestReg() {
        String tel = etTel.getText().toString();
        String verify = etVerify.getText().toString();
        String pwd = etPwd.getText().toString();

        if (!TextUtils.isEmpty(mType)) {
            if (mType.equals("regist")) {
                boolean isCheckOk = LoginBiz.checkReg(tel, verify, pwd);
                if (isCheckOk) {
                    LoginHttpBiz.reg(this, tel, pwd, verify);
                }
            } else {
                boolean isCheckOk = LoginBiz.checkReg(tel, verify, pwd);
                if (isCheckOk) {
                    if (verify.equals(mCode)) {
                        LoginHttpBiz.bind(this, tel, pwd, verify);
                    } else {
                        YcToastHelper.getInstance()._toast("验证码错误");
                    }
                }
            }
        }
    }

}
